package cn.itcast.itcaststore.web.servlet;

import cn.itcast.itcaststore.domain.User;
import cn.itcast.itcaststore.service.Impl.IUserService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.ref.ReferenceQueue;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/ModifyUserInfoServlet")
public class ModifyUserInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        User modify_user = new User();
        try {
            BeanUtils.populate(modify_user,parameterMap );
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        if (!modify_user.getPassword().equals("")){
            IUserService iUserService = new IUserService();
            iUserService.modifyUser(modify_user);
            //确保修改数据的回显
            User user = iUserService.getUser(modify_user.getUsername());
            request.getSession().setAttribute("user",user );

            request.setAttribute("modify_message","修改成功" );
            request.getRequestDispatcher("client/modifyuserinfo.jsp").forward(request,response );
        }else {
            request.setAttribute("modify_message","密码为空，不能修改" );
            request.getRequestDispatcher("client/modifyuserinfo.jsp").forward(request,response );
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
